package group2assignone.dao;

import group2assignone.model.Customer;
import group2assignone.view.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author group2
 */
public class LoginProxy implements ClientComms, Runnable {

    private Client client;
    private Customer cust = new Customer();
    boolean waiting = true;
    boolean successful = false;
    private static LoginProxy instance;

    private LoginProxy() {
        client = Client.getInstance();
    }

    public static LoginProxy getInstance() {
        if (instance == null) {
            instance = new LoginProxy();
        }
        return instance;
    }

    public boolean login(Customer cust) {
        this.cust = cust;
        Thread t = new Thread(new LoginThread());
        t.start();
        Thread t2 = new Thread(this);
        t2.start();
        //while(waiting){}
        return successful;
    }

    public void run() {
        LoadingView ldView = new LoadingView();
        ldView.go();
        while (waiting) {
            ldView.animate();
        }
        ldView.dispose();
    }

    public class LoginThread implements Runnable {

        public void run() {
            try {
                Thread.sleep(7000);
            } catch (InterruptedException ex) {
                Logger.getLogger(LoginProxy.class.getName()).log(Level.SEVERE, null, ex);
            }
            successful = client.login(cust);
            waiting = false;
        }
    }

    public void logout() {
        client.logout();
    }

    public ArrayList<String> requestLoggedIn() {
        return client.requestLoggedIn();
    }
}
